﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaxAssistantBase;

namespace ShiBieProject
{
    class solaInvoiceInfoHandle
    {
        /// <summary>
        /// 得出每张票据是销项票还是进项票
        /// </summary>
        /// <param name="idIOMList"></param>
        /// <param name="companyName"></param>
        /// <param name="taxNo"></param>
        public static void judgementInputBillOrOutputBill(List<InvoiiceOcrModel> idIOMList,string companyName,string taxNo) {
            int dnazchulinum = 0;
            foreach (InvoiiceOcrModel iom in idIOMList) {
                dnazchulinum++;
                try
                {
                    if (iom.invoiceOcrSonLists != null && iom.invoiceOcrSonLists.Count > 0)
                    {
                        setInitialValue(iom.invoiceOcrSonLists[0]);
                        string gfcompanyName = iom.invoiceOcrSonLists[0].gfcompanyname;
                        string gftaxnumber = iom.invoiceOcrSonLists[0].gftaxnumber;
                        string xfcompanyname = iom.invoiceOcrSonLists[0].xfcompanyname;
                        string xftaxnumber = iom.invoiceOcrSonLists[0].xftaxnumber;
                        //完全可以匹配上的
                        if (compareCompanyNameOrTaxNo(gfcompanyName, companyName))
                        {
                            iom.invoiceOcrSonLists[0].gfcompanyname = companyName;
                            iom.invoiceOcrSonLists[0].gftaxnumber = taxNo;
                            iom.invoiceOcrSonLists[0].isProviderOrCustomer = CompanyOrMoneyStateEnum.InPutInvoice;
                            iom.invoiceOcrSonLists[0].invoiceState = CompanyOrMoneyStateEnum.GfCompanyNamePerfectMatch;
                            continue;
                        }
                        if (compareCompanyNameOrTaxNo(gftaxnumber, taxNo))
                        {
                            iom.invoiceOcrSonLists[0].gfcompanyname = companyName;
                            iom.invoiceOcrSonLists[0].gftaxnumber = taxNo;
                            iom.invoiceOcrSonLists[0].isProviderOrCustomer = CompanyOrMoneyStateEnum.InPutInvoice;
                            iom.invoiceOcrSonLists[0].invoiceState = CompanyOrMoneyStateEnum.GfTaxNoPerfectMatch;
                            continue;
                        }
                        if (compareCompanyNameOrTaxNo(xfcompanyname, companyName))
                        {
                            iom.invoiceOcrSonLists[0].xfcompanyname = companyName;
                            iom.invoiceOcrSonLists[0].xftaxnumber = taxNo;
                            iom.invoiceOcrSonLists[0].isProviderOrCustomer = CompanyOrMoneyStateEnum.OutPutInvoice;
                            iom.invoiceOcrSonLists[0].invoiceState = CompanyOrMoneyStateEnum.XfCompanyNamePerfectMatch;
                            continue;
                        }
                        if (compareCompanyNameOrTaxNo(xftaxnumber, taxNo))
                        {
                            iom.invoiceOcrSonLists[0].xfcompanyname = companyName;
                            iom.invoiceOcrSonLists[0].xftaxnumber = taxNo;
                            iom.invoiceOcrSonLists[0].isProviderOrCustomer = CompanyOrMoneyStateEnum.OutPutInvoice;
                            iom.invoiceOcrSonLists[0].invoiceState = CompanyOrMoneyStateEnum.XfTaxNoPerfectMatch;
                            continue;
                        }
                        //模糊比对上的
                        if (vagueTaxNo(gftaxnumber, taxNo))
                        {
                            setGfCompanyNameAndTaxNo(companyName, taxNo, iom, CompanyOrMoneyStateEnum.InPutInvoice, CompanyOrMoneyStateEnum.GfTaxNoPartialMatch);
                            continue;
                        }
                        if (vagueCompanyName(gfcompanyName, companyName))
                        {
                            setGfCompanyNameAndTaxNo(companyName, taxNo, iom, CompanyOrMoneyStateEnum.InPutInvoice, CompanyOrMoneyStateEnum.GfCompanyNamePartialMatch);
                            continue;
                        }
                        if (vagueTaxNo(xftaxnumber, taxNo))
                        {
                            setXfCompanyNameAndTaxNo(companyName, taxNo, iom, CompanyOrMoneyStateEnum.OutPutInvoice, CompanyOrMoneyStateEnum.XfTaxNoPartialMatch);
                            continue;
                        }
                        if (vagueCompanyName(xfcompanyname, companyName))
                        {
                            setXfCompanyNameAndTaxNo(companyName, taxNo, iom, CompanyOrMoneyStateEnum.OutPutInvoice, CompanyOrMoneyStateEnum.XfCompanyNamePartialMatch);
                            continue;
                        }
                        if (iom.invoiceOcrSonLists[0].invoiceMark == "代开")
                        {
                            setXfCompanyNameAndTaxNo(companyName, taxNo, iom, CompanyOrMoneyStateEnum.OutPutInvoice, CompanyOrMoneyStateEnum.XfDaiKaiTaxNoPartialMatch);
                            continue;
                        }
                        iom.invoiceOcrSonLists[0].isProviderOrCustomer = CompanyOrMoneyStateEnum.NotMatch;
                        iom.invoiceOcrSonLists[0].invoiceState = CompanyOrMoneyStateEnum.NotMatch;
                        InvoiceOcrTool.setFeiQi(iom, CompanyOrMoneyStateEnum.NotMatch);
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    WsdLogger.yxlog("error", "第" + dnazchulinum + "条判断进销项票错误" + ex.Message);
                    continue;
                }
              
            }
        }

        private static void setInitialValue(InvoiceSonModel isonm) {
            isonm.invoiceDateInitialValue = isonm.invoiceDate;
            isonm.invoiceNumInitialValue = isonm.invoiceNum;
            isonm.invoicetypeInitialValue = isonm.invoicetype;
            isonm.gfcompanynameInitialValue = isonm.gfcompanyname;
            isonm.gftaxnumberInitialValue = isonm.gftaxnumber;
            isonm.moneyAmountsInitialValue = isonm.moneyAmounts;
            isonm.taxRateInitialValue = isonm.taxRate;
            isonm.taxAmountsInitialValue = isonm.taxAmounts;
            isonm.totalMoneyInitialValue = isonm.totalMoney;
            isonm.xfcompanynameInitialValue = isonm.xfcompanyname;
            isonm.xftaxnumberInitialValue = isonm.xftaxnumber;
        }
        /// <summary>
        /// 设置销方公司和税号
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="taxNo"></param>
        /// <param name="iom"></param>
        /// <param name="isProviderOrCustomer"></param>
        /// <param name="invoiceState"></param>
        private static void setXfCompanyNameAndTaxNo(string companyName, string taxNo, InvoiiceOcrModel iom,string isProviderOrCustomer,string invoiceState)
        {
            iom.invoiceOcrSonLists[0].xfcompanyname = companyName;
            iom.invoiceOcrSonLists[0].xftaxnumber = taxNo;
            iom.invoiceOcrSonLists[0].isProviderOrCustomer = isProviderOrCustomer;
            iom.invoiceOcrSonLists[0].invoiceState = invoiceState;
        }
        /// <summary>
        /// 设置购方公司和税号
        /// </summary>
        /// <param name="companyName"></param>
        /// <param name="taxNo"></param>
        /// <param name="iom"></param>
        /// <param name="isProviderOrCustomer"></param>
        /// <param name="invoiceState"></param>
        private static void setGfCompanyNameAndTaxNo(string companyName, string taxNo, InvoiiceOcrModel iom, string isProviderOrCustomer, string invoiceState)
        {
            iom.invoiceOcrSonLists[0].gfcompanyname = companyName;
            iom.invoiceOcrSonLists[0].gftaxnumber = taxNo;
            iom.invoiceOcrSonLists[0].isProviderOrCustomer = isProviderOrCustomer;
            iom.invoiceOcrSonLists[0].invoiceState = invoiceState;
        }
        /// <summary>
        /// 比较名称或税号
        /// </summary>
        /// <param name="compareCompanyName"></param>
        /// <param name="companyName"></param>
        /// <returns></returns>
        private static bool compareCompanyNameOrTaxNo(string compareCompanyName, string companyName) {
            if (compareCompanyName== companyName) {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 模糊比对税号
        /// </summary>
        /// <param name="vagueTaxNo"></param>
        /// <param name="TaxNo"></param>
        /// <returns></returns>
        private static bool vagueTaxNo(string vagueTaxNo, string TaxNo) {
            if (string.IsNullOrEmpty(vagueTaxNo)) {
                return false;
            }
            int  xiangsidu = InvoiceOcrTool.getTaxNum(vagueTaxNo, TaxNo);//获取税号相似度

            if (xiangsidu > ((vagueTaxNo.Length - 2) > 0 ? (vagueTaxNo.Length - 2) : 1000))
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 模糊比对名称
        /// </summary>
        /// <param name="vagueCompanyName"></param>
        /// <param name="CompanyName"></param>
        /// <returns></returns>
        private static bool vagueCompanyName(string vagueCompanyName, string CompanyName)
        {
            if (string.IsNullOrEmpty(vagueCompanyName))
            {
                return false;
            }
            int fengzhi = 0;
            int iss = InvoiceOcrTool.NameXianSiD(vagueCompanyName, CompanyName.Replace("公司", ""));//获取名称
            string comnames = (vagueCompanyName.Replace("公司", "")).Replace("有限", "");
            if (comnames.Length >= 6 && comnames.Length < 8)
            {
                fengzhi = 5;
            }
            else if (comnames.Length >= 8)
            {
                fengzhi = 7;
            }
            else if (comnames.Length < 6)
            {
                fengzhi = 3;
            }
            if (iss >= fengzhi)
            {
                return true;
            }
            return false;
        }
    }
}
